#pragma once
//view, 通常是拿到数据之后，要进行构建网页，渲染网页内容，展示给用户的
#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

#include "Oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template.html";

    class View
    {
    public:
        View(){}
        ~View(){}
    public:
        void AllExpandHtml(const std::vector<Question> &questions, std::string *html)
        {
            //形成路径
            std::string scr_html = template_path + "/all_questions.html";
            //形成数字典
            ctemplate::TemplateDictionary root("all_questions");
            for(const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }
            //获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(scr_html, ctemplate::DO_NOT_STRIP);
            //开始完成渲染功能
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const Question &q, std::string *html)
        {
            //形成路径
            std::string scr_path = template_path + "/one_question.html";
            //形成数字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("header", q.header);
            
            // pre_code 这个好像没有填充哦
            //获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(scr_path, ctemplate::DO_NOT_STRIP);
            //开始完成渲染功能
            tpl->Expand(html, &root);
        }
    };
}
